package de.home.raspi.timer;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class TimerTaskManagement {

   List<TimerTask>                    activeTimeTask;
   private static TimerTaskManagement timerTaskManagement;
   private Timer                      timer = new Timer();

   private TimerTaskManagement() {
      activeTimeTask = new ArrayList<TimerTask>();
   }

   public synchronized static TimerTaskManagement getInstance() {
      if ( timerTaskManagement == null )
         timerTaskManagement = new TimerTaskManagement();
      return timerTaskManagement;
   }

   public void addTimerTask(TimerTask timerTask) {
      long time = new Date().getTime();

      timer.schedule(new TimerTask() {

         @Override
         public void run() {
            System.out.println("task 1 start");
            try {
               Thread.sleep(5000);
            } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }
            System.out.println("task 1 end");
         }
      }, new Date(time + 2000));

      timer.schedule(new TimerTask() {

         @Override
         public void run() {
            System.out.println("task 2 start");
            try {
               Thread.sleep(5000);
            } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }
            System.out.println("task 2 end");
         }
      }, new Date(time + 2500));

      timer.schedule(new TimerTask() {

         @Override
         public void run() {
            System.out.println("task 3 start");
            try {
               Thread.sleep(5000);
            } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }
            System.out.println("task 3 end");
         }
      }, 6000);

   }

}
